package com.gold.rabbitmqdemo.study3;

import com.gold.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class WorkFairProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection= ConnectionUtils.getConnection();
        Channel channel=connection.createChannel();
        String queueName="test_work_fair_queue";
        //声明队列
        channel.queueDeclare(queueName,false,false,false,null);

        /**
         * 每个消费者发送确认消息之前，消息队列不发送下一个消息到消费者，一次只处理一个消息
         * 限制发送给同一个消费者不得超过一条消息
         */
        int preFetchCount=1;
        channel.basicQos(preFetchCount);


        for (int i = 0; i <50 ; i++) {
            String message="hello"+i;
            System.out.println(" work queue send :"+message);
            channel.basicPublish("",queueName,null,message.getBytes());

            try {
                Thread.sleep(i *5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }

        channel.close();
        connection.close();


    }

}
